﻿#region Header
/*
 * name：LanguageManager
 * author：Kain
 * date：11/22/2010 3:14:36 PM
 *********************************************
 * History:
 * $Header$
 * $History$
 */
#endregion

#region imported namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Globalization;
using Janus.Cores.IOC;
#endregion

namespace Janus.Cores.Globalization
{
    /// <summary>
    /// 资源管理
    /// </summary>
    public class RS 
    {
        IResourcesProvider _provider;
        static IResourcesProvider __defaultProvider;

        private RS(string resourceName)
        {

            if (Container.Current.IsRegistered<IResourcesProvider>())
                _provider = Container.Current.Resolve<IResourcesProvider>(
                    new ParameterOverride("resourceName", resourceName));
            else
                _provider = new EmptyResouceProvider(resourceName);

        }

        static RS()
        {
            if (Container.Current.IsRegistered<IResourcesProvider>())
                __defaultProvider = Container.Current.Resolve<IResourcesProvider>(new ParameterOverride("resourceName", string.Empty));
            else
                __defaultProvider = new EmptyResouceProvider(string.Empty);
        }

        /// <summary>
        /// 创建一个指定的资源
        /// </summary>
        /// <param name="resourceName">资源名称</param>
        /// <returns></returns>
        public static IResourcesProvider CreateResource(string resourceName)
        {
            return new RS(resourceName)._provider;
        }

        /// <summary>
        /// 获取资源中的文本
        /// </summary>
        /// <param name="name">文本名称</param>
        /// <returns>资源中的文本</returns>
        public static string GetString(string name)
        {
            return __defaultProvider.GetString(name);
        }

        /// <summary>
        /// 获取资源中的文本
        /// </summary>
        /// <param name="name">文本名称</param>
        /// <param name="culture"><see cref="System.Globalization.CultureInfo"/></param>
        /// <returns>资源中的文本</returns>
        public static string GetString(string name, CultureInfo culture)
        {
            return __defaultProvider.GetString(name, culture);
        }

        /// <summary>
        /// 获取资源
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>资源</returns>
        public static object GetObject(string name)
        {
            return __defaultProvider.GetObject(name);
        }

        /// <summary>
        /// 获取资源
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="culture"><see cref="System.Globalization.CultureInfo"/></param>
        /// <returns>资源</returns>
        public static object GetObject(string name, CultureInfo culture)
        {
            return __defaultProvider.GetObject(name, culture);
        }

        public static IEnumerator<object> GetEnumerator(CultureInfo culture = null) {
            foreach (var key in __defaultProvider.Keys) {
                if( culture != null)
                    yield return __defaultProvider.GetObject(key, culture);
                else
                    yield return __defaultProvider.GetObject(key);
            }
        }
 
        
    }
}
